package com.meixiang.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.meixiang.R;
import com.meixiang.entity.shopping.CartGoodsEntity;
import com.meixiang.entity.shopping.CartGroupEntity;
import com.meixiang.util.DensityUtil;
import com.meixiang.util.GlideHelper;
import com.meixiang.util.TextViewUtil;

import java.util.List;

/**
 * 购物车上显示商品的View
 *
 * @author LZRUI
 */
public class ShoppingCartGoodsView extends LinearLayout {

    private Context mContext;

    public ShoppingCartGoodsView(Context context) {
        this(context, null);
    }

    public ShoppingCartGoodsView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    @SuppressLint("NewApi")
    public ShoppingCartGoodsView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;
        setOrientation(VERTICAL);
    }

    /**
     * 获取布局上的控件
     */
    private View getView(String goodsName, String storePrice, String marketPrice, String specInfo, String goodsImage, int index) {
        View goodsLayout = LayoutInflater.from(mContext).inflate(R.layout.layout_shopping_cart_goods, this, false);
        ImageView ivGoodsLogo = (ImageView) goodsLayout.findViewById(R.id.iv_goods_logo);
        TextView tvGoodsName = (TextView) goodsLayout.findViewById(R.id.tv_goods_name);
        TextView tvGoodsSpecInfo = (TextView) goodsLayout.findViewById(R.id.tv_goods_spec_info);
        TextView tvGoodsNewPrice = (TextView) goodsLayout.findViewById(R.id.tv_goods_new_price);
        TextView tvGoodsOldPrice = (TextView) goodsLayout.findViewById(R.id.tv_goods_old_price);
        // TODO 设置图片
        GlideHelper.showImage(mContext, goodsImage, R.drawable.image_default_gray_bg, ivGoodsLogo);
        tvGoodsName.setText(goodsName);
        tvGoodsNewPrice.setText(String.format(mContext.getString(R.string.price_str), storePrice));
        TextViewUtil.setStrickoutText(tvGoodsOldPrice, String.format(mContext.getString(R.string.price_str), marketPrice));
        tvGoodsSpecInfo.setText(specInfo);

        if (index > 0) {
            // 如果当前index大于0的时候，需要设置marginTop = 20dp
            LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
            int marginTop = DensityUtil.dip2px(mContext, 20);
            params.setMargins(0, marginTop, 0, 0);
            goodsLayout.setLayoutParams(params);
        }

        return goodsLayout;
    }

    /**
     * 设置套装数据
     *
     * @param goodsList
     */
    public void setGoodsList(List<CartGroupEntity> goodsList) {
        // 清除子View
        removeAllViews();
        int len = goodsList.size();
        for (int i = 0; i < len; i++) {
            CartGroupEntity goods = goodsList.get(i);
            View commentView = getView(goods.getGoodsName(), goods.getGoodsStorePrice(), goods.getGoodsMarketPrice(), goods.getSpecInfoString(mContext), goods.getGoodsImage(), i);
            addView(commentView);
        }
    }

    /**
     * 设置单件商品
     *
     * @param goods
     */
    public void setGoods(CartGoodsEntity goods) {
        // 清除子View
        removeAllViews();
        View commentView = getView(goods.getGoodsName(), goods.getGoodsStorePrice(), goods.getGoodsMarketPrice(), goods.getSpecInfoString(mContext), goods.getGoodsImage(), 0);
        addView(commentView);
    }

}
